const Sequelize = require('sequelize');
const db = require('../db');

// 定义百度网盘课程模型
const baiduCourse = db.define('baiduCourse', {
	// 主键
	objectId: {
		type: Sequelize.STRING(20),
		allowNull: false,
		unique: true,
		primaryKey: true,
		comment: '主键ID'
	},

	name: {
		type: Sequelize.STRING(255),
		allowNull: false,
		comment: '课程名称（原始名称）'
	},

	displayName: {
		type: Sequelize.STRING(255),
		allowNull: true,
		comment: '显示名称（去除序号后的名称）'
	},

	order: {
		type: Sequelize.INTEGER,
		allowNull: true,
		defaultValue: 0,
		comment: '排序序号（从名称提取的数字）'
	},

	path: {
		type: Sequelize.STRING(500),
		allowNull: false,
		comment: '百度网盘路径'
	},

	fs_id: {
		type: Sequelize.STRING(50),
		allowNull: false,
		unique: true,
		comment: '百度网盘文件ID（唯一标识）'
	},

	updateTime: {
		type: Sequelize.DATE,
		allowNull: true,
		comment: '百度网盘更新时间'
	},

	createTime: {
		type: Sequelize.DATE,
		allowNull: true,
		comment: '百度网盘创建时间'
	},

	syncTime: {
		type: Sequelize.DATE,
		allowNull: true,
		defaultValue: Sequelize.NOW,
		comment: '同步到数据库的时间'
	},

	folderInfo: {
		type: Sequelize.TEXT,
		allowNull: true,
		comment: '百度网盘原始信息（JSON）'
	},

	isVisible: {
		type: Sequelize.BOOLEAN,
		allowNull: true,
		defaultValue: true,
		comment: '是否前台可见'
	},

	remark: {
		type: Sequelize.TEXT,
		allowNull: true,
		comment: '备注'
	},

	baiduDeskLink: {
		type: Sequelize.STRING(500),
		allowNull: true,
		comment: '百度网盘分享链接（同步时自动生成）'
	}
}, {
	tableName: 'baiduCourse',
	timestamps: false,
	indexes: [{
			name: 'idx_fs_id',
			unique: true,
			fields: ['fs_id']
		},
		{
			name: 'idx_order',
			fields: ['order']
		},
		{
			name: 'idx_sync_time',
			fields: ['syncTime']
		},
		{
			name: 'idx_visible',
			fields: ['isVisible']
		}
	]
});

// 如果需要同步表结构，取消注释以下代码
/*
(async () => {
	await baiduCourse.sync({
		alter: true
	});
	console.log("百度网盘课程表同步成功");
})();
*/

module.exports = baiduCourse;
